package com.gzc.web.action;

import java.lang.reflect.InvocationTargetException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.gzc.common.Const;
import com.gzc.model.User;
import com.gzc.service.IDepartmentService;
import com.gzc.service.IUserService;
import com.gzc.util.ActionUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.org.apache.commons.beanutils.BeanUtils;

@Controller("userAction")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User> {

	private User user;
	private IUserService userService;
	private IDepartmentService departmentService;
	private Integer depId;
	
	public Integer getDepId() {
		return depId;
	}
	public void setDepId(Integer depId) {
		this.depId = depId;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	@Autowired
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	@Autowired
	public void setDepartmentService(IDepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	
	@Override
	public User getModel() {
		if(null == user){
			user = new User();
		}
		return user;
	}
	
	//================用户的增删改查==========================
	
	public String addInput(){
		ActionContext.getContext().put("ds", this.departmentService.listAllDep());
		return SUCCESS;
	}
	
	public String add(){
		userService.add(user, depId);
		ActionUtil.setUrl(Const.USER_LIST_URL);
		return ActionUtil.REDIRECT;
	}
	
	/** 验证添加用户 **/
	public void validateAdd(){
		if(ActionUtil.isEmpty(user.getUsername())){
			this.addFieldError("username", "用户名不能为空");
		}
		if(this.hasFieldErrors()){
			this.addInput();
		}
	}
	
	public String delete() {
		userService.del(user.getId());
		ActionUtil.setUrl(Const.USER_LIST_URL);
		return ActionUtil.REDIRECT;
	}
	
	public String updateInput() throws IllegalAccessException, InvocationTargetException{
		User tu = this.userService.load(user.getId());
		BeanUtils.copyProperties(user, tu);
		ActionContext.getContext().put("ds", this.departmentService.listAllDep());
		
		return SUCCESS;
	}
	
	public String update(){
		User tu = this.userService.load(user.getId());
		tu.setEmail(this.user.getEmail());
		tu.setNickname(this.user.getNickname());
		tu.setType(this.user.getType());
		tu.setDepartment(this.user.getDepartment());
		
		this.userService.update(tu, depId);
		ActionUtil.setUrl(Const.USER_LIST_URL);
		return ActionUtil.REDIRECT;
	}
	
	public String updateSelfInput() throws IllegalAccessException, InvocationTargetException{
		 User tu = (User)ActionContext.getContext().getSession().get(Const.LOGIN_USER_KEY);
		 BeanUtils.copyProperties(user, tu);		 
		return SUCCESS;
	}
	
	public String updateSelf(){
		User tu = this.userService.load(user.getId());
		tu.setEmail(this.user.getEmail());
		tu.setNickname(this.user.getNickname());		
		
		this.userService.update(tu);
		ActionUtil.setUrl(Const.USER_SHOW_SELF_URL);
		return ActionUtil.REDIRECT;
	}
	
	public String list(){
		ActionContext.getContext().put("ds", this.departmentService.listAllDep());
		ActionContext.getContext().put("pages", this.userService.findUserByDep(depId));
		return SUCCESS;
	}
	
	public String show() throws IllegalAccessException, InvocationTargetException{
		User tu = this.userService.load(user.getId());
		BeanUtils.copyProperties(user, tu);
		return SUCCESS;
	}
	
	public String showSelf() throws IllegalAccessException, InvocationTargetException {				
		return updateSelfInput();
	}

}
